// packageB/pages/package/smart_box/clear_box/index.js
const mybehavior = require("../../../../../behaviors/requests")
Page({

    /**
     * 页面的初始数据
     */
    behaviors: [mybehavior],
    data: {
        baseUrl: "https://logisticsengineering.gree.com/smartg_api/storage_racks_manage",
        data_list: [],
        cabinet_id: "YS001",
        location_code: "",
    },
    // 获取数据
    getDatas() {
        let url = this.data.baseUrl + "/query_msg_by_location_code"
        let data = {}
        if (this.data.location_code) {
            data = {
                cabinet_id: this.data.cabinet_id,
                location_code: this.data.location_code,

            }
        } else {
            data = {
                cabinet_id: this.data.cabinet_id,
                location_code: { "$ne": "" }

            }
        }


        this.post(url, data).then(res => {

            if (res.code == 20000) {
                if (Object.keys(res.data).length != 0) {
                    console.log(res);
                    this.setData({
                        data_list: res.data
                    })
                } else {
                    wx.showToast({
                        title: '暂无数据',
                        icon: 'none'
                    })
                    this.setData({
                        data_list: []
                    })
                }

            } else {
                wx.showToast({
                    title: res.msg,
                    icon: 'none'
                })
            }
        }).catch(err => {
            console.log(err);

            wx.showToast({
                title: err.msg,
                icon: 'none'
            })
        })


    },


    // 扫描按钮
    onScan() {
        const self = this;
        wx.scanCode().then((res) => {
            const label_code = res.result;
            wx.showLoading({
                title: "loading"
            })
            console.log(label_code);
            setTimeout(function () {
                wx.hideLoading()
                self.setData({
                    location_code: label_code
                })
                self.getDatas()
            }, 500)
        }).catch(err => {
            console.log(err);
        });

    },



    //   点击清理标签刷新
    onConfirm(e) {
        let key = [e.currentTarget.dataset.row._id]
        let self = this
        wx.showModal({
            title: '',
            content: '受理编号：' + key,
            confirmText: '确认清理',
            confirmColor: '#0000FF',
            complete: (res) => {
                if (res.cancel) {

                }

                if (res.confirm) {
                    self.clearAll(key)

                }
            }
        })

    },

    // 清柜操作
    clearAll(key) {
        wx.showLoading({
            title: '处理中.....',
        })
        let _ids = key
        let url = this.data.baseUrl + "/take_out"
        let data = {
            _ids: _ids,
        }
        this.post(url, data).then(res => {

            if (res.code == 20000) {
                console.log(res);
                wx.showToast({
                    title: res.msg,
                    icon: 'none'
                })
                wx.hideLoading()
                this.getDatas()
            } else {
                wx.showToast({
                    title: res.msg,
                    icon: 'none'
                })
            }
        }).catch(err => {
            console.log(err);
            wx.showToast({
                title: err.msg,
                icon: 'none'
            })
        })
    },



    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.getDatas()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})